package com.example.smart.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.RectF
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatImageView
import com.example.lib.extend.dip2px
import com.example.lib.extend.getResourceColor
import com.example.lib.extend.sp2px

class UnReaderImageView @JvmOverloads constructor(context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0) : AppCompatImageView(context, attrs, defStyleAttr) {

    private val rect = RectF()
    private val textRect = Rect()
    private var unReaderCount = ""
    private var textWidth = 0
    private var textHeight = 0
    private var textMarginStart = 0
    private val mNormalSize by lazy {
        context.dip2px(16f)
    }

    private val dp6 by lazy {
        context.dip2px(7f)
    }

    private val mPaint by lazy {
        Paint().also {
            it.color = context.getResourceColor(com.example.lib.R.color.red_ff395d)
            it.isAntiAlias = true
        }
    }

    private val textPaint by lazy {
        Paint().also {
            it.color = context.getResourceColor(com.example.lib.R.color.white)
            it.textSize = context.sp2px(9).toFloat()
            it.isAntiAlias = true
            it.textAlign = Paint.Align.CENTER
        }
    }

    init {
        isClickable = true
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (unReaderCount.length == 1) {
            rect.left = dp6 + dp6.toFloat()
            rect.right = rect.left + textWidth
            rect.top = 0f
            rect.bottom = textHeight.toFloat()
            canvas.drawOval(rect, mPaint)
            canvas.drawText(unReaderCount, rect.left + textWidth / 2f, textHeight * 0.7f, textPaint)
        } else if (unReaderCount.length > 1) {
            rect.left = (width / 2).toFloat() + dp6
            rect.right = rect.left + textWidth
            rect.top = 0f
            rect.bottom = textHeight.toFloat()
            canvas.drawRoundRect(rect, (textHeight / 2).toFloat(), (textHeight / 2).toFloat(), mPaint)
            canvas.drawText(unReaderCount, (rect.left + textWidth / 2f), (textHeight * 0.7f).toFloat(), textPaint)
        }
    }

    fun setUnReaderCount(count: String) {
        unReaderCount = count
        textWidth = mNormalSize
        textHeight = mNormalSize
        if (count.length > 1) {
            textWidth = mNormalSize + context.dip2px(((count.length - 1) * 3).toFloat())
        }
        getTextWidth()
        invalidate()
    }

    fun setPaintColor(color: Int) {
        mPaint.color = context.getResourceColor(color)
    }

    fun setTextPaint(color: Int) {
        textPaint.color = context.getResourceColor(color)
    }

    private fun getTextWidth() {
        textPaint.getTextBounds(unReaderCount, 0, unReaderCount.length, textRect)
        textMarginStart = (textWidth - textRect.width()) / 2
    }
}